from typing import Collection
import easygui
import tkinter as tk
from tkinter import messagebox as mg
from hashlib import blake2b as hash_
import math
import os
import sys
import sqlite3
from reedsolo import RSCodec
import base64
import progressbar #progressbar2
def RsEnCode(msg,len=16):
    ecc=RSCodec(len)#纠错码字长度，可纠正len/2个误读
    return ecc.encode(bytearray(msg,'utf-8'))
def RsDecode(encode,len=16):
    ecc=RSCodec(len)
    data=ecc.decode(encode)
    return data[0].decode()
ecc=RSCodec(16)#纠错码字长度，可纠正len/2个误读
def rscodefactory(cursor, row):
    
    # print(row[0])
    # print(row[1])
    b64=base64.b64encode(ecc.encode(bytes.fromhex(row[1])))
    return (row[0],b64.decode())

db=sqlite3.connect("test.db")

cur=db.cursor()
curf=db.cursor()
curf.row_factory=rscodefactory
r=cur.execute("select count(*) from hash")
r=r.fetchone()
total=r[0]
n=0
'''
with progressbar.ProgressBar(max_value=total) as bar:
    while(n<total):
        bar.update(n)
        # print(n)
        r=curf.execute(f"select * from hash limit  {n},{n+100} ")
        r=r.fetchall()
        # print(r)
        n=n+100
'''
with progressbar.ProgressBar(max_value=total) as bar:
    while(n<total):
        bar.update(n)
        # print(n)
        r=curf.execute(f"select * from hash limit  {n},{n+100} ")
        r=r.fetchall()
        # print(r)
        n=n+100

# def getbmi():
#     field=["身高（m）","体重（Kg）"]
#     fieldValues = easygui.multenterbox("",fields=field)
#     bmi=float(fieldValues[1])/float(fieldValues[0])**2
#     if(bmi<=18.4):
#         r="偏瘦"
#     elif(bmi<=23.9):
#         r="正常"
#     elif(bmi<=27.9):
#         r="过重"
#     else:
#         r="肥胖"
#     return easygui.ccbox(f"你的BMI为：{bmi:.2f}\n结果：{r}",choices=("返回","退出"))

# while(getbmi()):
#     pass
def click1():
    if(T1.get()==""):
        mg.showerror("输入错误",message="请输入身高")
        return False
    if(T2.get()==""):
        mg.showerror("输入错误",message="请输入体重")
        return False
    sg=float(T1.get())/100
    tz=float(T2.get())
    bmi=tz/sg**2
    if(bmi<=18.4):
        r="偏瘦"
    elif(bmi<=23.9):
        r="正常"
    elif(bmi<=27.9):
        r="过重"
    else:
        r="肥胖"
    L3.configure(text=f"BMI:{bmi:.3f}\n{r}")
    return(bmi,r)

w=tk.Tk()
w.title="BMI计算"
w.geometry("400x300")
L1=tk.Label(w,text="请输入身高（cm）")
L1.grid(column=0,row=0)
T1=tk.Entry(w,width=20)
T1.grid(column=1,row=0)

L2=tk.Label(w,text="请输入体重（Kg）")
L2.grid(column=0,row=1)
T2=tk.Entry(w,width=20)
T2.grid(column=1,row=1)

B1=tk.Button(w,text="计算BMI", command=click1)
B1.grid(column=0,row=2)

L3=tk.Label(w,text="")
L3.grid(column=0,row=3)
w.mainloop()